

/* 
描述：定义点赞功能的相关后端接口 
完成人：吴悠 
完成时间: 2025/04/27
*/
package com.jiesenhe.qacommunity.controller;

import com.jiesenhe.qacommunity.pojo.Like;
import com.jiesenhe.qacommunity.pojo.LikeQuestion;
import com.jiesenhe.qacommunity.service.LikeService;
import com.jiesenhe.qacommunity.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import java.security.PublicKey;

import static com.alibaba.druid.sql.ast.SQLPartitionValue.Operator.List;

@RestController
@RequestMapping("/like")
public class LikeController {
    @Autowired
    LikeService likeService;

    @PostMapping("/getLikeStatus")
    public ResponseEntity<Object> getLikeStatus(@RequestBody Like like, HttpServletRequest request) {
        String token = request.getHeader("Token");
        if (!JwtUtil.checkToken(token)){
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }

        int result = likeService.getLikeStatus(like);

        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", result != 0 ? 1 : 0);
                put("msg", result != 0 ? "getLikeStatus success" : "no this LikeRecord");
                put("data", null);
            }
        });
    }

    @GetMapping("/getLikeQuestion")
    public ResponseEntity<Object> getLikeQuestion(@RequestParam int userId, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!JwtUtil.checkToken(token)){
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }

        List<LikeQuestion> resultData = likeService.getLikeQuestion(userId); // 假设方法返回 List<LikeQuestion>
        int result = resultData.size();

        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {{
            put("code", 1);
            put("msg", result != 0 ? "getLikeQuestion success" : "no this LikeRecord");
            put("data", resultData); // 直接传递实际数据
        }});
    }

    @PostMapping("/addLikeRecord")
    public ResponseEntity<Object> addLikeRecord(@RequestBody Like like, HttpServletRequest request) {
        String token = request.getHeader("Token");
        if (!JwtUtil.checkToken(token)) {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }
        try {
            int result = likeService.addLikeRecord(like);//like加
            int result2  = likeService.updateLikeStatus(like);//问题表或者回答表加
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", result != 0&&result2!= 0 ? 1 : 0);
                    put("msg", result != 0&&result2!= 0 ? "addLikeRecord success" : "addLikeRecord failed");
                    put("data", null);
                }
            });
        } catch (Exception e) {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "addLikeRecord failed");
                    put("data", null);
                }
            });
        }

    }

    @DeleteMapping("/deleteLikeRecord")
    public ResponseEntity<Object> deleteLikeRecord(@RequestBody Like like, HttpServletRequest request) {
        String token = request.getHeader("Token");
        if (!JwtUtil.checkToken(token)) {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }


        try {
            int result = likeService.deleteLikeRecord(like);
            int result2 = likeService.deleteLikeStatus(like);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", result != 0&&result2!= 0 ? 1 : 0);
                    put("msg", result != 0&&result2!= 0 ? "deleteLikeRecord success" : "deleteLikeRecord failed");
                    put("data", null);
                }
            });
        } catch (Exception e) {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "deleteLikeRecord failed");
                    put("data", null);
                }
            });
        }

    }

}
